package com.joshua.spring.controller;

import com.joshua.spring.entity.dto.JobAddDTO;
import com.joshua.spring.entity.dto.JobInstanceDTO;
import com.joshua.spring.entity.dto.JobKeyDTO;
import com.joshua.spring.entity.dto.JobUpdateDTO;
import com.joshua.spring.entity.form.AddTaskByCornForm;
import com.joshua.spring.entity.form.OptTaskForm;
import com.joshua.spring.entity.form.UpdateCornInfoForm;
import com.joshua.spring.entity.vo.GlobalResponse;
import com.joshua.spring.entity.vo.JobEntityVO;
import com.joshua.spring.service.inter.JobCtrlService;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * projectName dragon-cloud
 * package com.joshua.spring.controller
 * classname  JobCtrlController
 * <p>
 * description 任务调度控制器
 * </p>
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/9/13 9:46
 */
@Slf4j
@RestController
@RequestMapping("job")
public class JobCtrlController {
    @Autowired
    private JobCtrlService jobCtrlService;

    @PostMapping
    public GlobalResponse addTaskByCorn(@RequestBody AddTaskByCornForm form) {
        JobAddDTO dto = new JobAddDTO();
        BeanUtils.copyProperties(form, dto);
        String result;
        if (StringUtils.isNotBlank(form.getCorn())) {
            result = jobCtrlService.addJobByCorn(dto);
        } else {
            result = jobCtrlService.addJobByJobTime(dto);
        }
        return GlobalResponse.buildSuccess(result);
    }

    @PutMapping
    public GlobalResponse optTaskStatus(@RequestBody OptTaskForm form) {
        JobKeyDTO dto = new JobKeyDTO();
        BeanUtils.copyProperties(form, dto);
        return GlobalResponse.buildSuccess(jobCtrlService.optJob(dto, form.getOptType()));
    }

    @PutMapping("corn")
    public GlobalResponse updateJobCornInfo(@RequestBody UpdateCornInfoForm form) {
        JobUpdateDTO dto = new JobUpdateDTO();
        BeanUtils.copyProperties(form, dto);
        return GlobalResponse.buildSuccess(jobCtrlService.updateJobCornInfo(dto));
    }

    @GetMapping("runType/{runType}:")
    public GlobalResponse<List<JobEntityVO>> queryAllJob(@PathVariable String runType) {
        String all = "run";
        List<JobEntityVO> result = null;
        List<JobInstanceDTO> jobs;
        if (all.equals(runType)) {
            jobs = jobCtrlService.queryRunJob();
        } else {
            jobs = jobCtrlService.queryAllJob();
        }
        if (!CollectionUtils.isEmpty(jobs)) {
            result = jobs.stream().map(e -> {
                JobEntityVO vo = new JobEntityVO();
                BeanUtils.copyProperties(e, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return GlobalResponse.buildSuccess(result);
    }

}
